package com.chat.websocket;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {
    private final FriendRequestHandler friendRequestHandler;
    private final ChatMessageHandler chatMessageHandler;
    private final GroupChatMessageHandler groupChatMessageHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(friendRequestHandler, "/websocket/friend/*")
                .setAllowedOrigins("*"); // 允许跨域

        registry.addHandler(chatMessageHandler, "/websocket/chat/*")
                .setAllowedOrigins("*"); // 允许跨域

        registry.addHandler(groupChatMessageHandler, "/websocket/groupChat/*")
                .setAllowedOrigins("*"); // 允许跨域
    }
}
